#!/bin/bash
# - mode
# - runtime (dontshow)
# - option (dontshow)
# - multi
# - rtcwake (dont show)
# - idle_time (dont show)
# - debug (dont show)

. $LKP_SRC/lib/upload.sh
. $LKP_SRC/lib/http.sh

export PATH=/lkp/benchmarks/analyze-suspend:$PATH

cmd=sleepgraph.py

echo options: mode: $mode, runtime: $runtime, rtcwake: $rtcwake, multi: $multi, option: $option
options=""
[ -n "$mode" ]		|| mode='freeze'
[ -n "$multi" ] 	|| multi=1
[ -n "$runtime" ]	|| runtime=600
[ -n "$rtcwake" ]	|| rtcwake=30
[ -n "$idle_time" ]	|| idle_time=0

[[ "$mode" = "freeze" && $(which turbostat) ]] && option="$option -turbostat"

options="-m $mode $option -rtcwake $rtcwake -sync -info $multi"

[ $(which xset) ] && options="$options -display on"

# use -gzip and -skiphtml to reduce the memory usage
options="$options -gzip -skiphtml"
# skip udelay kprobe to reduce data
options="$options -skipkprobe udelay"

################################################################################
# To avoid OOM, before setting ftrace buffer size, we need to reserve memory for
# 1. analyze_suspend processing
# 2. saving result files because we're using ramdisk
################################################################################

total_mem=$(cat /proc/meminfo | grep MemFree | awk '{print $2}')
# reserve 400M for processing
process_mem=$((400 * 1024))

case $options in
	*" -f "*) result_mem=$((500 * 1024));;
	*" -dev "*) result_mem=$((50 * 1024));;
	*) result_mem=$((5 * 1024));;
esac

ftrace_mem=$((total_mem - process_mem - result_mem))

echo $option | grep "\-f" > /dev/null
if [ $? -eq 0 ]
then
	fmem=$((500 * 1024))
else
	fmem=$((50 * 1024))
fi

[ $ftrace_mem -gt $fmem ] && ftrace_mem=$fmem
# in case we get negative value for ftrace_mem
[ $ftrace_mem -lt 0 ] && ftrace_mem=$fmem

options="$options -bufsize $ftrace_mem"
################################################################################

f_log=./test.log
f_dmesg=./dmesg.log
f_lkp_output=./analyze-suspend

echo options: $options >> $f_log
echo analyze_suspend test started at: $(date +%F) $(date +%T) >> $f_log
echo analyze_suspend test started

sleep $idle_time

for ite in $(seq $multi)
do
	#check network status
	retry=30
	while true
	do
		ping -c 1 -W 1 $LKP_SERVER > /dev/null
		[ $? -eq 0 ] && break
		echo $(date +%T): network not ready >> $f_log
		retry=$((retry - 1))
		[ $retry -eq 0 ] && break
	done

	# restart network if network is not back in 30 seconds
	if [ $retry -eq 0 ]
	then
		$LKP_SRC/bin/run-ipconfig
		ping -c 1 -W 1 $LKP_SERVER > /dev/null
		if [ $? -ne 0 ]
		then
			echo network down >> $f_log
			[ -n "$debug" ] && sleep 72000
			exit 1
		fi
		echo network recovered by $LKP_SRC/bin/run-ipconfig >> $f_log
	fi

	echo $(date +%T): suspending-$ite/$multi >> $f_log
	if [ -n "$debug" ]
	then
		set_job_state "suspending_debug-$ite/$multi" > /dev/null
	else
		set_job_state "suspending-$ite/$multi" > /dev/null
	fi

	#upload log files in case system crashes
	dmesg > $f_dmesg
	upload_files -t result $f_log
	upload_files -t result $f_dmesg

	#make sure the test either PASS or FAIL, no INCOMPLETE
	echo "test started" > $f_lkp_output
	upload_files $f_lkp_output

	#upload and delete previous test results to save ramdisk space
	f_result=$(ls ./ | grep "suspend-")
	if [ -n "$f_result" ]
	then
		echo $(date +%T): uploading test result $f_result ... >> $f_log
		upload_files -t result $f_result
		rm -rf $f_result
		echo "$(date +%T): done ..." >> $f_log
	fi

	#commands to supend and resume
	echo $(date +%T): $cmd $options
	echo $(date +%T): $cmd $options >> $f_log
	$cmd $options
#	[ $? -ne 0 ] && exit 1
	echo $(date +%T): resume back, return value $? !
	echo $(date +%T): resume back, return value $? ! >> $f_log

	sleep $idle_time
done

echo "analyze_suspend test done"

rm $f_lkp_output
upload_files -t result $f_log
upload_files -t result ./suspend-*
